home *** CD-ROM | disk | FTP | other *** search
/ Scene 96 / Scene 96 International Edition (Zyklop Software) (Disc 2) (1997).iso / misc / coding / midas060 / samples / midpnt / archiver.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-24  |  5.6 KB  |  214 lines

  1. /*
  2.  *      Archivers.cpp
  3.  *
  4.  * MIDAS Module Player for Windows NT archiver support
  5.  *
  6.  * Copyright 1996 Petteri Kangaslampi
  7. */
  8.  
  9. #define WIN32_LEAN_AND_MEAN
  10. #include <windows.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <process.h>
  14. #include <dos.h>
  15.  
  16. #include "midasdll.h"
  17.  
  18. #ifdef __VC32__
  19. #include <io.h>
  20. #endif
  21.  
  22. #include "MidpNT.h"
  23. #include "archivers.h"
  24.  
  25.  
  26.  
  27. int             numArchives = 4;
  28. Archive         archives[4] =
  29.     {
  30.         { ".mdz", "unzip -o \"%s\" -d %s" },
  31.         { ".s3z", "unzip -o \"%s\" -d %s" },
  32.         { ".xmz", "unzip -o \"%s\" -d %s" },
  33.         { ".zip", "unzip -o \"%s\" -d %s" }
  34.     };
  35.  
  36.  
  37.  
  38.  
  39. /****************************************************************************\
  40. *
  41. * Function:     int IsArchive(char *fileName);
  42. *
  43. * Description:  Checks if a file is an archive (based on the extension)
  44. *
  45. * Input:        char *fileName          file name
  46. *
  47. * Returns:      1 if the file is an archive, 0 if not
  48. *
  49. \****************************************************************************/
  50.  
  51. int IsArchive(char *fileName)
  52. {
  53.     int         i;
  54.     char        ext[_MAX_EXT];
  55.  
  56.     _splitpath(fileName, NULL, NULL, NULL, ext);
  57.  
  58.     for ( i = 0; i < numArchives; i++ )
  59.     {
  60.         if ( !stricmp(ext, archives[i].extension) )
  61.             return 1;
  62.     }
  63.  
  64.     return 0;
  65. }
  66.  
  67.  
  68.  
  69.  
  70. /****************************************************************************\
  71. *
  72. * Function:     MIDASmodule LoadArchive(char *fileName);
  73. *
  74. * Description:  Loads a module from an archive
  75. *
  76. * Input:        char *fileName          file name
  77. *
  78. * Returns:      MIDAS module handle for the module or NULL if failed
  79. *
  80. \****************************************************************************/
  81.  
  82. MIDASmodule LoadArchive(char *fileName)
  83. {
  84.     int         i;
  85.     char        ext[_MAX_EXT];
  86.     char        file[_MAX_FNAME];
  87.     char        *decompress;
  88.     char        commandLine[_MAX_PATH + 128];
  89.     STARTUPINFO startInfo;
  90.     PROCESS_INFORMATION procInfo;
  91.     DWORD       exitCode;
  92.     WIN32_FIND_DATA findData;
  93.     HANDLE      findHandle;
  94.     DWORD       creationFlags;
  95.     SECURITY_ATTRIBUTES secAttr = { sizeof(SECURITY_ATTRIBUTES), NULL, FALSE};
  96.     int         fileFound;
  97.     MIDASmodule module;
  98.  
  99.     AddTextLine("Decompressing module");
  100.     SendMessage(mainWinHandle, WM_SETTEXT, 0,
  101.         (LPARAM) "MidpNT - Decompressing...");
  102.  
  103.     GetCurrentDirectory(MAX_PATH+127, commandLine);
  104.     if ( SetCurrentDirectory(unzipDir) )
  105.     {
  106.         SetCurrentDirectory(commandLine);
  107.         sprintf(commandLine, "Directory \"%s\" already exists", unzipDir);
  108.         MessageBox(NULL, commandLine, "Decompression error",
  109.             MB_ICONERROR | MB_OK);
  110.         return NULL;
  111.     }
  112.  
  113.     _splitpath(fileName, NULL, NULL, file, ext);
  114.  
  115.     decompress = NULL;
  116.     for ( i = 0; i < numArchives; i++ )
  117.     {
  118.         if ( !stricmp(ext, archives[i].extension) )
  119.             decompress = archives[i].decompress;
  120.     }
  121.  
  122.     if ( decompress == NULL )
  123.         Panic("Decompress: Unknown extension!");
  124.  
  125.     sprintf(commandLine, decompress, fileName, unzipDir);
  126.     AddTextLine(commandLine);
  127.  
  128.     startInfo.cb = sizeof(STARTUPINFO);
  129.     startInfo.lpReserved = NULL;
  130.     startInfo.lpDesktop = NULL;
  131.     startInfo.lpTitle = commandLine;
  132.     startInfo.dwFlags = STARTF_USESHOWWINDOW;
  133.     startInfo.wShowWindow = SW_HIDE;
  134.     startInfo.cbReserved2 = 0;
  135.     startInfo.lpReserved2 = NULL;
  136.  
  137.     creationFlags = CREATE_NEW_CONSOLE | NORMAL_PRIORITY_CLASS;
  138.  
  139.     if ( !CreateProcess(NULL, commandLine, NULL, NULL, FALSE,
  140.         creationFlags, NULL, NULL,
  141.         &startInfo, &procInfo) )
  142.     {
  143.         sprintf(commandLine, "Decompress: CreateProcess failed: %i",
  144.             GetLastError());
  145.         Panic(commandLine);
  146.     }
  147.  
  148.     exitCode = STILL_ACTIVE;
  149.     while ( exitCode == STILL_ACTIVE )
  150.     {
  151.         Sleep(250);
  152.         if ( !GetExitCodeProcess(procInfo.hProcess, &exitCode) )
  153.         {
  154.             sprintf(commandLine, "Decompress: GetExitCodeProcess failed: %i",
  155.                 GetLastError());
  156.             Panic(commandLine);
  157.         }
  158.     }
  159.  
  160.     if ( exitCode != 0 )
  161.     {
  162.         sprintf(commandLine, "Decompressor failed: %i",
  163.             exitCode);
  164.         MessageBox(NULL, commandLine, "Decompression error",
  165.             MB_OK | MB_ICONERROR);
  166.         return NULL;
  167.     }
  168.  
  169.     AddTextLine("Loading Module");
  170.     SendMessage(mainWinHandle, WM_SETTEXT, 0, (LPARAM) "MidpNT - Loading...");
  171.  
  172.     sprintf(commandLine, "%s\\*.*", unzipDir, file);
  173.  
  174.     if ( (findHandle = FindFirstFile(commandLine, &findData))
  175.           == INVALID_HANDLE_VALUE )
  176.     {
  177.         MessageBox(NULL, "Decompressed file not found", "Decompression error",
  178.             MB_OK | MB_ICONERROR);
  179.         return NULL;
  180.     }
  181.  
  182.     fileFound = 0;
  183.  
  184.     do
  185.     {
  186.         if ( !(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
  187.         {
  188.             sprintf(commandLine, "%s\\%s", unzipDir, findData.cFileName);
  189.  
  190.             if ( (module = MIDASloadModule(commandLine)) != NULL )
  191.             {
  192.                 fileFound = 1;
  193.             }
  194.  
  195.             if ( !DeleteFile(commandLine) )
  196.             {
  197.                 MessageBox(NULL, "Couldn't delete file",
  198.                     "Decompression error", MB_OK | MB_ICONERROR);
  199.                 return NULL;
  200.             }
  201.         }
  202.     } while ( (!fileFound) && FindNextFile(findHandle, &findData) );
  203.  
  204.     FindClose(findHandle);
  205.  
  206.     RemoveDirectory(unzipDir);
  207.  
  208.     if ( fileFound )
  209.         return module;
  210.  
  211.     MessageBox(NULL, "No valid module files found", "Decompression error",
  212.         MB_OK | MB_ICONERROR);
  213.     return NULL;
  214. }